In your final repo, there should be an R markdown file that organizes all computational steps for evaluating your proposed Facial Expression Recognition framework.
This file is currently a template for running evaluation experiments. You should update it according to your codes but following precisely the same structure.
if(!require("EBImage")){
source("https://bioconductor.org/biocLite.R")
biocLite("EBImage")
}
if(!require("R.matlab")){
install.packages("R.matlab")
}
if(!require("readxl")){
install.packages("readxl")
}
if(!require("dplyr")){
install.packages("dplyr")
}
if(!require("readxl")){
install.packages("readxl")
}
if(!require("ggplot2")){
install.packages("ggplot2")
}
if(!require("caret")){
install.packages("caret")
}
library(R.matlab)
library(readxl)
library(dplyr)
library(EBImage)
library(ggplot2)
library(caret)
set.seed(0)
setwd("~/Google Drive (yw3285@columbia.edu)/RA/FER/Project3-FacialEmotionRecognition/doc")
# here replace it with your own path or manually set it in RStudio to where this rmd file is located.
# use relative path for reproducibility
Training images are located in data/train_images folder. The file names are in the format of XX_XXX_XXXX; the first 2 digits represent emotion index, the following 3 digits represent identity(person) index, and the last four digits can be ignored. In current step, we extract paths to images and fiducial points, and corresponding emotion indices and identity indices.
#form paths of training images
image.path <- paste("../data/train_images",
list.files("../data/train_images"),
sep="/")
#form paths of fiducial points corresponding to training images
tmp <- gsub(image.path, pattern = 'jpg', replacement = 'mat')
point.path <- gsub(tmp, pattern = 'images', replacement = 'points')
#extract emotion index from paths
emotion_idx <- substr(image.path, 22, 23)
emotion_idx <- as.numeric(ifelse(as.numeric(emotion_idx < 10), substr(emotion_idx, 2, 2), emotion_idx))
identity <- as.numeric(substr(image.path, 25, 27))
emotion_table <- read_xls("../data/AU_annotation_all_subjects.xls")
info <- data.frame(stringsAsFactors = F, image.path = image.path, point.path = point.path, identity = identity, emotion_idx = emotion_idx) %>% inner_join(emotion_table, by = c('emotion_idx' = 'idx')) %>% mutate(Index = 1:length(image.path))
head(info)
In this chunk, we have a set of controls for the evaluation experiments.
run.cv=TRUE # run cross-validation on the training set
K <- 5 # number of CV folds
run.feature.train=TRUE # process features for training set
run.test=TRUE # run evaluation on an independent test set
run.feature.test=TRUE # process features for test set
Using cross-validation or independent test set evaluation, we compare the performance of models with different specifications. In this Starter Code, we tune parameter k (number of neighbours) for KNN.
k = c(5,11,21,31,41,51)
model_labels = paste("KNN with K =", k)
#train-test split
n <- nrow(info)
n_train <- round(n*(4/5), 0)
train_idx <- sample(info$Index, n_train, replace = F)
test_idx <- setdiff(info$Index,train_idx)
If you choose to use extract features from images, such as using Gabor filter, R memory will exhaust all images are read together. The solution is to repeat reading a smaller batch and process them.
EBImage::readImage(image.path[1])
## Image
## colorMode : Color
## storage.mode : double
## dim : 1000 750 3
## frames.total : 3
## frames.render: 1
##
## imageData(object)[1:5,1:6,1]
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 0.5960784 0.5921569 0.5921569 0.5882353 0.5921569 0.5960784
## [2,] 0.5882353 0.5843137 0.5803922 0.5803922 0.5803922 0.5843137
## [3,] 0.5843137 0.5803922 0.5725490 0.5686275 0.5686275 0.5764706
## [4,] 0.5882353 0.5843137 0.5725490 0.5686275 0.5686275 0.5764706
## [5,] 0.5960784 0.5921569 0.5843137 0.5803922 0.5803922 0.5882353
Fiducial points are stored in matlab format. In this step, we read them and store them in a list.
readMat.matrix <- function(path){
#print(path)
return(round(readMat(path)[[1]],0))
}
#load fiducial points
fiducial_pt_list <- lapply(point.path, readMat.matrix)
The follow plots show how pairwise distance between fiducial points can work as feature for facial emotion recognition.
The third column is the distributions of vertical distances between right mouth corner(50) and the midpoint of the upper lip(52). For example, the distance of an happy face tends to be shorter than that of a sad face.
feature.R should be the wrapper for all your feature engineering functions and options. The function feature( ) should have options that correspond to different scenarios for your project and produces an R object that contains features and responses that are required by all the models you are going to evaluate later.
feature.Rfiducial_pt_list <- lapply(point.path, readMat.matrix)
source("../lib/feature.R")
tm_feature_train <- NA
if(run.feature.train){
tm_feature_train <- system.time(dat_train <- feature(fiducial_pt_list, train_idx))
}
tm_feature_test <- NA
if(run.feature.train){
tm_feature_test <- system.time(dat_test <- feature(fiducial_pt_list, test_idx))
}
save(dat_train, file="../output/feature_train.RData")
save(dat_test, file="../output/feature_test.RData")
Call the train model and test model from library.
train.R and test.R should be wrappers for all your model training steps and your classification/prediction steps.
train.Rtest.ROutput: training model specification
In this Starter Code, we use KNN to do classification.
#source("../lib/train.R") Since knn does not need to train, I comment this line.
source("../lib/test_knn.R")
source("../lib/cross_validation_knn.R")
if(run.cv){
err_cv <- matrix(0, nrow = length(k), ncol = 2)
for(i in 1:length(k)){
cat("k=", k[i], "\n")
err_cv[i,] <- cv.function(dat_train, K, k[i])
save(err_cv, file="../output/err_cv.RData")
}
}
Visualize cross-validation results.
if(run.cv){
load("../output/err_cv.RData")
err_cv <- as.data.frame(err_cv)
colnames(err_cv) <- c("mean_error", "sd_error")
err_cv$k = as.factor(k)
err_cv %>%
ggplot(aes(x = k, y = mean_error,
ymin = mean_error - sd_error, ymax = mean_error + sd_error)) +
geom_crossbar() +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
}
if(run.cv){
model_best <- k[which.min(err_cv[,1])]
}
par_best <- list(k = model_best)
#tm_train=NA
#tm_train <- system.time(fit_train <- train(dat_train, par_best))
#save(fit_train, file="../output/fit_train.RData")
tm_test=NA
if(run.test){
load(file="../output/fit_train.RData")
tm_test <- system.time(pred <- test(model_best, dat_test))
}
accu <- mean(dat_test$emotion_idx == pred)
cat("The accuracy of model:", model_labels[which.min(err_cv[,1])], "is", accu*100, "%.\n")
## The accuracy of model: KNN with K = 21 is 27.6 %.
library(caret)
confusionMatrix(pred, dat_test$emotion_idx)
## Confusion Matrix and Statistics
##
## Reference
## Prediction 1 2 3 5 6 7 8 9 10 11 12 13 14 15 16 18 21 22 23 24 25
## 1 10 0 6 3 1 3 3 2 0 2 1 2 3 1 0 3 0 3 1 0 1
## 2 0 21 0 0 0 0 0 2 5 0 0 0 0 0 0 0 0 0 0 7 1
## 3 3 0 5 2 0 0 0 1 1 4 0 2 2 0 0 2 0 1 3 2 0
## 5 1 0 1 5 0 2 0 0 0 2 2 1 3 0 0 0 0 0 0 0 2
## 6 0 0 0 0 17 0 1 1 0 0 0 0 0 0 2 0 2 2 0 0 0
## 7 1 1 0 1 0 5 1 0 0 1 3 1 1 0 0 1 1 0 0 0 0
## 8 0 0 0 0 1 1 4 0 0 2 0 0 0 0 1 1 3 2 2 1 1
## 9 0 1 0 0 0 0 0 12 0 0 0 0 0 0 0 0 1 0 0 1 0
## 10 0 5 0 0 0 0 1 0 6 0 0 0 1 0 0 0 0 0 1 0 2
## 11 1 0 1 3 0 3 1 0 1 4 4 3 3 0 0 0 0 0 1 0 2
## 12 1 0 0 5 0 2 1 0 0 1 8 4 2 0 0 0 0 0 1 0 0
## 13 0 0 1 2 0 2 0 0 0 1 5 4 0 0 0 4 0 0 1 0 0
## 14 0 0 0 3 0 0 1 0 0 4 1 3 2 2 0 0 0 0 2 1 2
## 15 0 0 0 0 2 0 1 0 0 0 0 1 1 12 2 0 3 4 1 0 2
## 16 0 0 0 1 3 0 2 0 0 1 0 1 0 3 3 1 1 2 1 2 2
## 18 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 6 0 1 0 0 0
## 21 1 0 0 0 3 0 1 2 0 0 0 0 0 0 0 2 4 5 2 0 0
## 22 0 0 0 0 2 0 0 0 0 0 0 0 0 0 1 1 3 5 0 1 0
## 23 0 0 2 1 2 0 4 0 0 0 0 0 0 1 3 2 2 1 2 1 1
## 24 0 1 0 0 1 1 1 1 0 0 0 0 0 3 0 0 0 2 1 0 1
## 25 1 0 1 0 0 0 2 0 2 0 3 0 2 3 2 0 0 0 4 3 3
## 26 1 0 0 0 0 1 1 0 0 0 0 0 1 0 1 0 0 0 1 0 1
## Reference
## Prediction 26
## 1 3
## 2 1
## 3 3
## 5 0
## 6 0
## 7 3
## 8 0
## 9 0
## 10 0
## 11 1
## 12 0
## 13 1
## 14 2
## 15 3
## 16 0
## 18 0
## 21 0
## 22 1
## 23 2
## 24 5
## 25 2
## 26 0
##
## Overall Statistics
##
## Accuracy : 0.276
## 95% CI : (0.2372, 0.3174)
## No Information Rate : 0.064
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 0.2416
##
## Mcnemar's Test P-Value : NA
##
## Statistics by Class:
##
## Class: 1 Class: 2 Class: 3 Class: 5 Class: 6 Class: 7
## Sensitivity 0.5000 0.7241 0.2941 0.1923 0.5312 0.2381
## Specificity 0.9208 0.9660 0.9462 0.9705 0.9829 0.9687
## Pos Pred Value 0.2083 0.5676 0.1613 0.2632 0.6800 0.2500
## Neg Pred Value 0.9779 0.9827 0.9744 0.9563 0.9684 0.9667
## Prevalence 0.0400 0.0580 0.0340 0.0520 0.0640 0.0420
## Detection Rate 0.0200 0.0420 0.0100 0.0100 0.0340 0.0100
## Detection Prevalence 0.0960 0.0740 0.0620 0.0380 0.0500 0.0400
## Balanced Accuracy 0.7104 0.8451 0.6201 0.5814 0.7571 0.6034
## Class: 8 Class: 9 Class: 10 Class: 11 Class: 12
## Sensitivity 0.1600 0.5714 0.4000 0.1818 0.2963
## Specificity 0.9684 0.9937 0.9794 0.9498 0.9641
## Pos Pred Value 0.2105 0.8000 0.3750 0.1429 0.3200
## Neg Pred Value 0.9563 0.9814 0.9814 0.9619 0.9600
## Prevalence 0.0500 0.0420 0.0300 0.0440 0.0540
## Detection Rate 0.0080 0.0240 0.0120 0.0080 0.0160
## Detection Prevalence 0.0380 0.0300 0.0320 0.0560 0.0500
## Balanced Accuracy 0.5642 0.7826 0.6897 0.5658 0.6302
## Class: 13 Class: 14 Class: 15 Class: 16 Class: 18
## Sensitivity 0.1818 0.09524 0.4800 0.2000 0.2609
## Specificity 0.9644 0.95616 0.9579 0.9588 0.9958
## Pos Pred Value 0.1905 0.08696 0.3750 0.1304 0.7500
## Neg Pred Value 0.9624 0.96017 0.9722 0.9748 0.9654
## Prevalence 0.0440 0.04200 0.0500 0.0300 0.0460
## Detection Rate 0.0080 0.00400 0.0240 0.0060 0.0120
## Detection Prevalence 0.0420 0.04600 0.0640 0.0460 0.0160
## Balanced Accuracy 0.5731 0.52570 0.7189 0.5794 0.6283
## Class: 21 Class: 22 Class: 23 Class: 24 Class: 25
## Sensitivity 0.2000 0.1786 0.08333 0.0000 0.1429
## Specificity 0.9667 0.9809 0.95378 0.9647 0.9478
## Pos Pred Value 0.2000 0.3571 0.08333 0.0000 0.1071
## Neg Pred Value 0.9667 0.9527 0.95378 0.9607 0.9619
## Prevalence 0.0400 0.0560 0.04800 0.0380 0.0420
## Detection Rate 0.0080 0.0100 0.00400 0.0000 0.0060
## Detection Prevalence 0.0400 0.0280 0.04800 0.0340 0.0560
## Balanced Accuracy 0.5833 0.5798 0.51856 0.4823 0.5453
## Class: 26
## Sensitivity 0.0000
## Specificity 0.9852
## Pos Pred Value 0.0000
## Neg Pred Value 0.9452
## Prevalence 0.0540
## Detection Rate 0.0000
## Detection Prevalence 0.0140
## Balanced Accuracy 0.4926
Note that the accuracy is not high but is better than that of ramdom guess(4.5%).
Prediction performance matters, so does the running times for constructing features and for training the model, especially when the computation resource is limited.
cat("Time for constructing training features=", tm_feature_train[1], "s \n")
## Time for constructing training features= 1.348 s
cat("Time for constructing testing features=", tm_feature_test[1], "s \n")
## Time for constructing testing features= 0.197 s
#cat("Time for training model=", tm_train[1], "s \n")
cat("Time for testing model=", tm_test[1], "s \n")
## Time for testing model= 57.217 s